home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / Regex / src / Regex.C < prev    next >
C/C++ Source or Header  |  1991-06-14  |  5KB  |  223 lines

  1. #include <RJS/String.h>
  2. #include "Regex.h"
  3.  
  4. #include "GNUregex.h"
  5. // Regex stuff
  6.  
  7.  RJS_Regex::RJS_Regex() 
  8.     compile_error="RE: not intialized"; 
  9.     buf=0;
  10.     reg=0;
  11.     changed=0;
  12.  
  13.  RJS_Regex::RJS_Regex(const RJS_Regex &re) : RJS_String(re.cptr())
  14. {
  15.   initialize(re.cptr(), re.length(), (int) re.buf->fastmap,re.buf->allocated,
  16.             re.buf->translate);
  17. }
  18.  
  19.  RJS_Regex::RJS_Regex(const RJS_String& s, int fast , int bufsize, 
  20.              const char* transtable ) : RJS_String(s)
  21. {
  22.   initialize(s.cptr(), s.length(), fast, bufsize, transtable);
  23. }
  24.  
  25.  RJS_Regex::RJS_Regex(const char *s, int fast, int bufsize, 
  26.              const char* transtable) : RJS_String(s)
  27. {
  28.   initialize(s, -1, fast, bufsize, transtable);
  29. }
  30.  
  31. RJS_Regex::~RJS_Regex()
  32. {
  33.   if (buf) {
  34.        delete(buf->buffer);
  35.        delete(buf->fastmap);
  36.   }
  37.   delete(buf);
  38.   delete(reg);
  39. }
  40.  
  41. void RJS_Regex::initialize(const char* t, int tlen, int fast, int bufsize, 
  42.                        const char* transtable)
  43. {
  44.   if (tlen < 0) tlen = RJS_String::length(t);
  45.   buf = new re_pattern_buffer;
  46.   reg = new re_registers;
  47.   if (fast)
  48.     buf->fastmap = new char[256];
  49.   else
  50.     buf->fastmap = 0;
  51.   buf->translate = (char*)transtable;
  52.   if (tlen > bufsize)
  53.     bufsize = tlen;
  54.   buf->allocated = bufsize;
  55.   buf->buffer = new char [buf->allocated];
  56.   compile_error=re_compile_pattern((char*)t, tlen, buf);
  57.   if (!compile_error) if (fast) re_compile_fastmap(buf);
  58.   changed=0;
  59. }
  60.  
  61. int RJS_Regex::match_info(int& start, int& length, int nth) const
  62. {
  63.   if ((unsigned)(nth) >= RE_NREGS)
  64.     return 0;
  65.   else
  66.   {
  67.     start = reg->start[nth];
  68.     length = reg->end[nth] - start;
  69.     return start >= 0 && length >= 0;
  70.   }
  71. }
  72.  
  73. int RJS_Regex::search(const char* s, int& matchlen, int len,int startpos) const
  74. {
  75.   int matchpos, pos, range;
  76.   if (changed) compile();
  77.   if (compile_error) { matchlen=0; return -1; }
  78.   if (len<0) len=RJS_String::length(s);
  79.   if (startpos >= 0)
  80.   {
  81.     pos = startpos;
  82.     range = len - startpos;
  83.   }
  84.   else
  85.   {
  86.     pos = len + startpos;
  87.     range = -pos;
  88.   }
  89.   matchpos = re_search_2(buf, 0, 0, (char*)s, len, pos, range, reg, len);
  90.   if (matchpos >= 0)
  91.     matchlen = reg->end[0] - reg->start[0];
  92.   else
  93.     matchlen = 0;
  94.   return matchpos;
  95. }
  96.  
  97. int RJS_Regex::search(const RJS_String & s, int& matchlen) const
  98. {
  99.   int matchpos, pos, range;
  100.   int len=s.length();
  101.   if (changed) compile();
  102.   if (compile_error) { matchlen=0; return -1; }
  103.   pos = 0;
  104.   range = len;
  105.   matchpos = re_search_2(buf, 0, 0, (char *) s.cptr(), len, pos, range, reg, len);
  106.   if (matchpos >= 0)
  107.     matchlen = reg->end[0] - reg->start[0];
  108.   else
  109.     matchlen = 0;
  110.   return matchpos;
  111. }
  112.  
  113. int RJS_Regex::match(const char *s, int len, int p) const
  114. {
  115.   if (changed) compile();
  116.   if (compile_error) { return 0; }
  117.   if (len<0) len=RJS_String::length(s);
  118.   if (p < 0)
  119.   {
  120.     p += len;
  121.     if (p >= len)
  122.       return 0;
  123.     return re_match_2(buf, 0, 0, (unsigned char*)s, p, 0, reg, p);
  124.   }
  125.   else if (p >= len)
  126.     return 0;
  127.   else
  128.     return re_match_2(buf, 0, 0, (unsigned char*)s, len, p, reg, len);
  129. }
  130.  
  131. int RJS_Regex::match(const RJS_String &s) const
  132. {
  133.   int len=s.length();
  134.   if (changed) compile();
  135.   if (compile_error) { return 0; }
  136.   if (len==0) return 0;
  137.   return re_match_2(buf, 0, 0, (unsigned char*)s.cptr(), len, 0, reg, len);
  138. }
  139.  
  140. int RJS_Regex::compile()
  141. {
  142.   int fast    =  (int)buf->fastmap;
  143.   int alloc   = buf->allocated;
  144.   char *trans = buf->translate;
  145.  
  146.   if (buf) { 
  147.     delete(buf->buffer);
  148.     delete(buf->fastmap);
  149.   }
  150.   delete(buf);
  151.   delete(reg);
  152.  
  153.   initialize(cptr(),length(), fast, alloc,trans);
  154.  
  155.   return ok();
  156. }
  157.  
  158. void RJS_Regex::operator=(const RJS_Regex &re)
  159. {
  160.   if (buf) {
  161.       delete(buf->buffer);
  162.       delete(buf->fastmap);
  163.   }
  164.   delete(buf);
  165.   delete(reg);
  166.  
  167.    (RJS_String &) *this = (RJS_String &) (re);
  168.  
  169.    initialize(re.cptr(), re.length(), (int) re.buf->fastmap,re.buf->allocated,
  170.             re.buf->translate);
  171. }
  172.  
  173. void RJS_Regex::operator=(const char *s)
  174. {
  175.   if (buf) {
  176.      delete(buf->buffer);
  177.      delete(buf->fastmap);
  178.   }
  179.   delete(buf);
  180.   delete(reg);
  181.  
  182.    (RJS_String &) *this = s;
  183.   initialize(s, -1, 0,40,0); // SHOULD FIX!!! fast, bufsize, transtable);
  184. }
  185.  
  186. void RJS_Regex::operator=(const RJS_String &s)
  187. {
  188.   if (buf) {
  189.       delete(buf->buffer);
  190.       delete(buf->fastmap);
  191.   }
  192.   delete(buf);
  193.   delete(reg);
  194.  
  195.    (RJS_String &) *this = s;
  196.   initialize(s.cptr(), s.length(), 0,40,0); // fast, bufsize, transtable);
  197. }
  198.  
  199. int RJS_Regex::ok() const
  200. {
  201.   if (changed) compile();
  202.   int v = buf != 0;             // have a regex buf
  203.   v &= buf->buffer != 0;        // with a pat
  204.   return v && (compile_error==0);
  205. }
  206.  
  207. const RJS_Regex RXwhite("[ \n\t\r\v\f]+", 1);
  208. const RJS_Regex RXoptwhite("[ \n\t\r\v\f]*", 1);
  209. const RJS_Regex RXnonwhite("[^ \n\t\r\v\f]+", 1);
  210. const RJS_Regex RXint("-?[0-9]+", 1);
  211. const RJS_Regex RXdouble("-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?", 1, 200);
  212. const RJS_Regex RXalpha("[A-Za-z]+", 1);
  213. const RJS_Regex RXlower("[a-z]+", 1);
  214. const RJS_Regex RXupper("[A-Z]+", 1);
  215. const RJS_Regex RXalphanum("[0-9A-Za-z]+", 1);
  216. const RJS_Regex RXid("[A-Za-z_][A-Za-z0-9_]*", 1);
  217. const RJS_Regex RXstr("\"[^\"]*\"",1);
  218. // RXstrq is a string with \" in it. returns string with \" included
  219. const RJS_Regex RXstrq("\"\\([^\"\\]\\|\\\\\"\\|\\\\\\)*\"",1); 
  220.  
  221.